package com.collabscm.waf.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 查询参数，保存查询条件，此VO可以被用于数据库过滤、对象排序过滤等。
 *
 * @author Zeng Raoxing
 */
public class QueryParameters extends ValueObject {
	private static final long serialVersionUID = 1L;		
	/**
	 * 开始位置
	 */
	protected int start;
	/**
	 * 查询数量
	 */
	protected int size;
	/**
	 * 过滤条件，key为参数名称，value为过滤条件，注意：本类不支持非命名的where子句
	 */
	protected Map<String, Object> filters;
	/**
	 * 排序条件
	 */
	protected Order order;
	
	// Setter/Getter
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getMaxResults() {
		return start + size;
	}

	public void setSize(int size) {
		this.size = size;
	}
	
	/**
	 * 增加名称对应的参数过滤条件
	 * 
	 * @param paramName
	 * @param paramValue
	 */
	public void addFilter(String paramName, Object paramValue) {
		if(filters == null) {
			filters = new HashMap<String, Object>();
		}
		filters.put(paramName, paramValue);
	}
	
	/**
	 * 设置排序，注意此处仅支持根据一个参数过滤的情况，如果需要多种一起过来，那么可提供子类覆盖此方法。
	 * 
	 * @param paramName   排序的参数名
	 * @param isAscending 是否为升序
	 */
	public void setOrder(String paramName, boolean isAscending) {
		this.order = new Order(paramName, isAscending);
	}
	
	public Map<String, Object> getFilters() {
		return this.filters;
	}
	
	public Order getOrder() {
		return this.order;
	}
	
	/**
	 * 自定义的toString方法，便于进行缓存和提示
	 */
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("start:").append(start).append(",");
		sb.append("size:").append(size).append(",");
		sb.append("filters:").append(filters).append(",");
		sb.append("order:").append(order);
		return sb.toString();
	}
	
	//Inner Calss
	/**
	 * 排序
	 *
	 * @author Zeng Raoxing
	 */
	public static class Order implements Serializable {
		private static final long serialVersionUID = 1L;
		/**
		 * 排序的参数名
		 */
		private String paramName;
		/**
		 * 是否升序
		 */
		private boolean isAscending;
		public Order(String paramName, boolean isAscending) {
			this.paramName = paramName;
			this.isAscending = isAscending;
		}
		public String getParamName() {
			return paramName;
		}
		public boolean isAscending() {
			return isAscending;
		}
		@Override
		public String toString() {
			return paramName + "," + isAscending;
		}
	}

}
